<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {

    public function _initialize(){
        if(!session('admin')){
            $this->error('无权限',U('Login/login'));
            exit;
        }
    }
    
  	public function userlist(){
	
		$usersmodel = M('users'); // 实例化User对象
		$count = $usersmodel->count();// 查询满足要求的总记录数
		$Page = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show = $Page->show();// 分页显示输出

		//进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $usersmodel->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('users',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display(); // 输出模板
	}
    
    

    public function bsUserList(){
    
        $usersmodel=M('users');
       $local_users=$usersmodel->order('id desc')->select();
        $this->users=$local_users;
        $this->display();
    
    
    }
    
    public function users() {
        $usersmodel=M('users');
        $local_users=$usersmodel->order('id desc')->select();
        $this->users=$local_users;
        $this->display();
    
     ;
    }
    
    
    public function del($id){
        //生成一个Users模型 
           $usersModel=M('Users');
       // 调用模型的方法删除数据
         //  $usersModel -> delete($id);
      // delete from think_users where id=$id
        if($usersModel -> delete($id))
        {
            //删除成功
            $this ->success('删除成功');
        }else {
            $this ->error('删除失败');
            //删除失败
        }
    }
    
    public function setstar($id){
        
    	$userModel=m('Users');
        $data['isstar']=1;
        
        if($userModel->where("id=$id")->save($data)){
        	$this->success('success');
        }else{
        	$this->error('failed');
        }
    	
    }
    
    public function unstar($id){
    
        $userModel=m('Users');
        $userModel -> id=$id; 
        $userModel -> isstar=0;
        $userModel -> isfirst=0;
       
        if($userModel->save()){
            $this->success('success');
        }else{
            $this->error('failed');
        }
         
    }
    
    public function setfirst($id){
    
        $userModel=m('Users');
        $userModel -> id=$id;
        $userModel -> isstar=1;
        $userModel -> isfirst=1;
         
        if($userModel->save()){
            $this->success('success');
        }else{
            $this->error('failed');
        }
         
    }
    
    public function unfirst($id){
    
        $userModel=m('Users');
        $userModel -> id=$id;
        $userModel -> isstar=0;
        $userModel -> isfirst=0;
         
        if($userModel->save()){
            $this->success('success');
        }else{
            $this->error('failed');
        }
         
    }
    
    public function updateUserInfo($id){
        $userModel=m('Users');
        $this->user=$userModel->find($id);
        $this->display();
        //将查询到的用户信息user传递给模版updateuserinfo。html

   }
   
   public function update(){
    	//$data = $_POST;
    	//$data = I(post.);
    	//create(); 利用post数据创建一个对象
    	$userModel = m('Users');
    	$userModel->create();
    	
    	if($_FILES['photo']['error']==0){
    	    $photoinfo=$this->upload();
    	    $userModel->photo=$photoinfo['photo']['savename'];
    	}
    	 
    	if($userModel->save())
    		{
    			$this->success('修改成功');
    		}else{
    			$this->error('修改失败');
    		}
    }
   
 public function upload(){
               $upload = new \Think\Upload();// 实例化上传类
               $upload->maxSize = 3145728 ;// 设置附件上传大小
               $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
               $upload->rootPath = './Public/Uploads/photo/'; // 设置附件上传根目录
             //$upload->savePath = ''; // 设置附件上传（子）目录
               $upload->autoSub=false;
            // 上传文件
                $info = $upload->upload();
                 if(!$info) {// 上传错误提示错误信息
                 $this->error($upload->getError());
                  }else{// 上传成功
            //$this->success('上传成功！');
            	return $info;
                                        }   
            }

            
 /*public function addUser(){
     // 1.接收从add.html提交过来的数据，修改数据库
     //$data=$-POST
     //$data=I{post.}
     //create() 利用$_POST数组创建一个对象
     // 2.注意：单独处理图像的上传 以及photo字段的赋值
     // 3.保存到数据库  add（）方法 
		$userModel = D('Users');
    	if($userModel->create()){
    	if($_FILES['photo']['error']==0){
    		$photoinfo = $this->upload();
    		$userModel->photo = $photoinfo['photo']['savename'];
    	}

    	if($userModel->add())
    		{
    			$this->success('添加成功');
    		}else{
    			$this->error('添加失败');
    		}
        }
        else{
            $this->error($userModel->getError());
        }
*/

            public function addUser()
            {
                $userModel = D('Users');
                if($userModel->create()){
                    /*if($_FILES['photo']['error']==0){
                     $photoinfo = $this->upload();
                     $userModel->photo = $photoinfo['photo']['savename'];
                     }*/
            
                    if($_FILES['photo']['error']==0){
                        $photoinfo = $this->img();
                        $userModel->photo = $photoinfo;
            
                    }
            
                    if($userModel->add())
                    {
                        $this->success('添加成功');
                    }else{
                        $this->error('添加失败');
                    }
                }
                else{
                    $this->error($userModel->getError());
                }
            
            
            }
            function img()
            {
                if($_FILES['photo']['error']==0){
                    $photoinfo = $this->upload();
                    $filePath = "./Public/Uploads/photo/";
                    $oldFileName = $photoinfo['photo']['savename'];
                    $oldFileFullName = $filePath."\\".$oldFileName;
                    $newFileName = 'thumb_'.$oldFileName;
                    $newFileFullName = $filePath."\\".$newFileName;
            
            
            
                    //$imgFile = $photoinfo['photo']['rootpath'].'\\'.$photoinfo['photo']['savename'];
                    $image = new \Think\Image();
                    $image->open($oldFileFullName);
                    $image->thumb(100,100)->save($newFileFullName);
                    return $newFileName;
                }
            }
            
          
	}
     
 
